#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import shutil

def delete_proj_files():
    """
    NOTE: 必须从源码顶层目录运行这个脚本;
    没有注释的行会被删除,
    被注释的行会被保留。
    """
    current_path = os.getcwd()

    remove_list = [
        # "components/atcmd/",  # 保留
        "components/ble/",      # 删除
        # "components/fota/",   # 保留

        # "components/fs/kv/",     # 保留
        # "components/fs/nvds/",   # 保留
        # "components/fs/partition_mgr/",  # 保留

        # "components/kernel/",    # 源文件保留
        # "components/libc/",      # 源文件保留
        "components/libc/CMakeLists.txt",  # 删除

        # "components/net/dhcpd/",         # 保留
        # "components/net/httpsclient/",   # 保留
        # "components/net/iperf/",         # 保留
        # "components/net/lwip-2.0.3/",    # 保留
        # "components/net/mbedtls/",       # 保留
        # "components/net/ping/",          # 保留

        # "components/qcloud_iot_c_sdk/",  # 保留

        # "components/serial/",              # 保留

        # "components/smartlink/airkiss/",   # 保留
        # "components/smartlink/smartcfg/",  # 保留
        # "components/smartlink/softapcfg/", # 保留

        # "components/utils/",             # 保留

        "components/wifi/wifi_cfg/",     # NOTE: 必须删除
        "components/wifi/wifi_if/",      # NOTE: 必须删除
        "components/wifi/wifi_mac/",     # NOTE: 必须删除
        "components/wifi/CMakeLists.txt",     # NOTE: 必须删除

        # "components/wifi/wifi_manager/", # 保留
        # "components/wifi/wifi_port/",    # 保留
        # "components/wifi/wifi.h",       # 保留

        # "lib/arm_cortexM4lf_math.lib"       # 先保留

        # "mcu/CMSIS_5.3.0/",              # 保留

        # 某个客户需要 GPIO 源码，因此需要保留GPIO相关的源文件(编译库的时候已经把GPIO源文件开放了)
        # "mcu/driver_ln882x/inc/hal",    # 保留
        # "mcu/driver_ln882x/inc/ll",     # 保留
        # "mcu/driver_ln882x/inc/reg/reg_gpio.h",    # 删除其他的（只保留GPIO相关的源文件）
        "mcu/driver_ln882x/inc/reg/reg_adc.h",
        "mcu/driver_ln882x/inc/reg/reg_aes.h",
        "mcu/driver_ln882x/inc/reg/reg_cache.h",
        "mcu/driver_ln882x/inc/reg/reg_dma.h",
        "mcu/driver_ln882x/inc/reg/reg_efuse.h",
        "mcu/driver_ln882x/inc/reg/reg_i2c.h",
        "mcu/driver_ln882x/inc/reg/reg_i2s.h",
        "mcu/driver_ln882x/inc/reg/reg_mac_pcu_reg.h",
        "mcu/driver_ln882x/inc/reg/reg_pwm.h",
        "mcu/driver_ln882x/inc/reg/reg_qspi.h",
        "mcu/driver_ln882x/inc/reg/reg_rf_p0.h",
        "mcu/driver_ln882x/inc/reg/reg_rf_p1.h",
        "mcu/driver_ln882x/inc/reg/reg_rtc.h",
        "mcu/driver_ln882x/inc/reg/reg_sdio.h",
        "mcu/driver_ln882x/inc/reg/reg_spim.h",
        "mcu/driver_ln882x/inc/reg/reg_spim2.h",
        "mcu/driver_ln882x/inc/reg/reg_spis.h",
        "mcu/driver_ln882x/inc/reg/reg_sysc_awo.h",
        "mcu/driver_ln882x/inc/reg/reg_sysc_cmp.h",
        "mcu/driver_ln882x/inc/reg/reg_timer.h",
        "mcu/driver_ln882x/inc/reg/reg_trng.h",
        "mcu/driver_ln882x/inc/reg/reg_uart.h",
        "mcu/driver_ln882x/inc/reg/reg_wdt.h",

        # "mcu/driver_ln882x/src/hal",    # 保留

        # "mcu/driver_ln882x/src/ll/ll_gpio.c",     # 只保留gpio，其他删除
        "mcu/driver_ln882x/src/ll/ll_adc.c",
        "mcu/driver_ln882x/src/ll/ll_aes.c",
        "mcu/driver_ln882x/src/ll/ll_boot_header.c",
        "mcu/driver_ln882x/src/ll/ll_cache.c",
        "mcu/driver_ln882x/src/ll/ll_dma.c",
        "mcu/driver_ln882x/src/ll/ll_efuse.c",
        "mcu/driver_ln882x/src/ll/ll_i2c.c",
        "mcu/driver_ln882x/src/ll/ll_i2s.c",
        "mcu/driver_ln882x/src/ll/ll_pwm.c",
        "mcu/driver_ln882x/src/ll/ll_qspi.c",
        "mcu/driver_ln882x/src/ll/ll_rtc.c",
        "mcu/driver_ln882x/src/ll/ll_sdio.c",
        "mcu/driver_ln882x/src/ll/ll_sleep.c",
        "mcu/driver_ln882x/src/ll/ll_spim.c",
        "mcu/driver_ln882x/src/ll/ll_spim2.c",
        "mcu/driver_ln882x/src/ll/ll_spis.c",
        "mcu/driver_ln882x/src/ll/ll_syscon.c",
        "mcu/driver_ln882x/src/ll/ll_timer.c",
        "mcu/driver_ln882x/src/ll/ll_trng.c",
        "mcu/driver_ln882x/src/ll/ll_uart.c",
        "mcu/driver_ln882x/src/ll/ll_wdt.c",

        # "mcu/ln88xx/",                   # 保留
        "mcu/CMakeLists.txt",                   # 删除

        'project/ATE_Test/',             # 删除
        'project/ble_host_lib/',         # 删除
        'project/bootload/',             # 删除
        'project/lib_build/',            # 删除
        'project/lowpower/',             # 删除
        "project/peripheral_driver_test/",   # 删除

        "project/wifi_mcu_airkiss_demo/keil_ln882x_dev/",    # 删除
        "project/wifi_mcu_basic_example/keil_ln882x_dev/",   # 删除

        "project/wifi_mcu_misc_demo/",   # 删除

        "project/wifi_mcu_ota_demo/keil_ln882x_dev/",    # 删除

        "project/wifi_mcu_qcloud_misc_demo/keil_ln882x_dev/",    # 删除

        "project/wifi_mcu_rf_test/",     # 必须删

        "project/wifi_mcu_smartconfig_demo/keil_ln882x_dev/",
        "project/wifi_mcu_softapconfig_demo/keil_ln882x_dev/",
        "project/wifi_mcu_tcp_demo/keil_ln882x_dev/",

        "project/wifi_slave/",           # 删除
        "project/wifi_slave_sdk/",       # 删除
        "project/wifi_track_develop/",   # 删除

        'tools/ota_tools/',             # 删除
        'tools/src/debug_host/',        # 删除
        'tools/src/FactoryDownloadTool/',        # 删除
        'tools/src/fpu_patch/',        # 删除
    ]

    # 检查文件夹或者文件夹是否存在，存在之后删除
    for path in remove_list:
        temp_path = os.path.join(current_path, path)
        print(temp_path)
        if not os.path.exists(temp_path):
            continue
        if os.path.isdir(temp_path):
            if os.listdir(temp_path):
                print("    |")
                print "    ------>",
                print("full dir deleted...")
                shutil.rmtree(temp_path)
            else:
                print("    |")
                print "    ------>",
                print("empty dir deleted...")
                os.removedirs(temp_path)
        elif os.path.isfile(temp_path):
            print("    |")
            print "    ------>",
            print("file deleted...")
            os.remove(temp_path)
        else:
            pass


def delete_this_script():
    abspath = __file__
    os.remove(abspath)


if __name__ == "__main__":
    print("\r\n")
    print("----------------------------------------")

    delete_proj_files()
    delete_this_script()


    print("----------------------------------------")
    print("\r\n")
